﻿var data = new WinJS.Binding.List([
        { title: "Sample1", description: "This is Sample One. It's only a Sample" },
        { title: "Sample2", description: "This is Sample Two. It's only another Sample" }]);

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/views/list/list.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.

            document.getElementById("cmdAdd").addEventListener("click", addItem, false);
            document.getElementById("cmdDelete").addEventListener("click", doClickDelete, false);
            document.getElementById("cmdEdit").addEventListener("click", doClickEdit, false);


            //// Hide selection group of commands
            CategoryAppBar.winControl.hideCommands(CategoryAppBar.querySelectorAll('.categoryBar'));
            CategoryAppBar.winControl.disabled = true;


            // Initialize List

            listView.winControl.itemDataSource = data.dataSource;
            listView.winControl.itemTemplate = mediumListTemplate;

            listView.winControl.forceLayout();
            listView.winControl.addEventListener("selectionchanged", itemSelect, false);

            listView.winControl.itemDataSource.getCount().done(function (itemCount) {

                // Set the initial selection in the ListView
                var initialSelection = (itemCount > 0) ? 0 : [];
                listView.winControl.selection.set(initialSelection);
            });

            runEnterPageAnimation();

        }
    });

    function itemSelect(evt) {
        if (listView.winControl.selection.count() == 0) {
            CategoryAppBar.winControl.disabled = true;
            CategoryAppBar.winControl.hideCommands(CategoryAppBar.querySelectorAll('.categoryBar'));
            CategoryAppBar.winControl.sticky = false;
            CategoryAppBar.winControl.hide();
        }
        else {
            //Set DetailPane
            listView.winControl.selection.getItems().done(function (items) {

                // TODO: Change display?
                detailHeader.innerText = items[0].data.title;

                detailContent.style.color = "black";
                detailContent.innerText = items[0].data.description;
            });
            //Show CategoryBar
            CategoryAppBar.winControl.disabled = false;
            CategoryAppBar.winControl.showCommands(CategoryAppBar.querySelectorAll('.categoryBar'));
            CategoryAppBar.winControl.sticky = true;
            CategoryAppBar.winControl.show();
        }
    }

    function onPointerDownAddProperty(evt) {
        WinJS.UI.Animation.pointerDown(AddProperty);
    }

    function onPointerUpAddProperty(evt) {
        WinJS.UI.Animation.pointerUp(AddProperty);
    }


    function runEnterPageAnimation() {
        mainsection.style.overflow = "hidden";
        // Animate the application's entrance in the number of stages chosen by the user
        // Use the recommended offset by leaving the offset argument empty to get the best performance
        var enterContent;
        WinJS.Promise.timeout(2000);
        enterContent = WinJS.UI.Animation.enterContent(mainsection, null);

        enterContent.done(
            function () {
                mainsection.style.overflow = "auto";
            });
    }

    // Adds a new Property to the end of the collection
    function addItem() {
        //TODO
        WinJS.Navigation.navigate("/pages/views/care/care.html", null);
    }

    // Delete Item
    function doClickDelete() {
        //TODO
    }

    //Edit Item
    function doClickEdit(evt) {
        //TODO
        WinJS.Navigation.navigate("/pages/views/care/care.html", null);
    }

    function onPointerDownSelect(evt) {
        WinJS.UI.Animation.pointerDown(evt.currentTarget);

        //var hasClass = WinJS.Utilities.hasClass(evt.currentTarget.children[0], "TaskAddCategoryFlexChildSelected");
        //if (!hasClass) {
        //    WinJS.Utilities.addClass(evt.currentTarget.children[0], "TaskAddCategoryFlexChildSelected");
        //    WinJS.Utilities.removeClass(evt.currentTarget.children[0], "TaskAddCategoryFlexChildItem");
        //    current = evt.currentTarget;
        //    CategoryAppBar.winControl.disabled = false;
        //    CategoryAppBar.winControl.hideCommands(CategoryAppBar.querySelectorAll('.categoryBar'));
        //    CategoryAppBar.winControl.showCommands(CategoryAppBar.querySelector('#cmdDelete'));
        //    CategoryAppBar.winControl.sticky = true;
        //    CategoryAppBar.winControl.show();
        //}
        //else {
        //    WinJS.Utilities.removeClass(evt.currentTarget.children[0], "TaskAddCategoryFlexChildSelected");
        //    WinJS.Utilities.addClass(evt.currentTarget.children[0], "TaskAddCategoryFlexChildItem");

        //    CategoryAppBar.winControl.disabled = true;
        //    CategoryAppBar.winControl.hideCommands(CategoryAppBar.querySelectorAll('.categoryBar'));
        //    CategoryAppBar.winControl.sticky = false;
        //    CategoryAppBar.winControl.hide();
        //}

    }

    function onPointerUpSelect(evt) {
        WinJS.UI.Animation.pointerUp(evt.currentTarget);
    }

})();
